#!/bin/bash
#2020年7月24日21:07:32
#auto change server ip
#by author www.jfedu.net
#######################
ETH_IP="$1"
ETH_DNS="8.8.8.8"
ETH_NAME="ifcfg-ens33"
ETH_MASK="255.255.255.0"
ETH_BAK="/data/backup/`date +%Y%m%d%H%M`"
ETH_DIR="/etc/sysconfig/network-scripts"
ETH_GATEWAY="$(echo $ETH_IP|awk -F. '{print $1"."$2"."$3}').254"
if [ $# -eq 0 ];then
	echo -e "\033[32m----------------\033[0m"
	echo -e "\033[32mUsage:{/bin/sh $0 1.1.1.1|1.1.1.2|help}\033[0m"
	exit 1
fi
cd $ETH_DIR/
ls -l ${ETH_NAME}
mkdir -p $ETH_BAK
\cp ${ETH_NAME} $ETH_BAK
grep -ai "dhcp" ${ETH_NAME}
if [ $? -eq 0 ];then
	sed -i 's/dhcp/static/g' ${ETH_NAME}	
	cat>>${ETH_NAME} <<-EOF
	IPADDR=${ETH_IP}
	NETMASK=${ETH_MASK}
	GATEWAY=${ETH_GATEWAY}
	DNS1=${ETH_DNS}
	EOF
	grep -aiE "BOOT|IPADDR|NETMASK|GATEWAY|DNS" ${ETH_NAME}
	#service network restart
else
	echo -e "\033[32m----------------\033[0m"
	grep -aiE "BOOT|IPADDR|NETMASK|GATEWAY|DNS" ${ETH_NAME}
	echo
	read -p "The ${ETH_NAME} IPaddr already static,Please sure change it ? " INPUT
	if [ $INPUT == "yes" -o $INPUT == "y" -o $INPUT == "YES" -o $INPUT == "Yes" ];then	
		sed -i -e '/IPADDR/d' -e '/NETMASK/d' -e '/GATEWAY/d' -e '/DNS/d' ${ETH_NAME}
		cat>>${ETH_NAME} <<-EOF
		IPADDR=${ETH_IP}
		NETMASK=${ETH_MASK}
		GATEWAY=${ETH_GATEWAY}
		DNS1=${ETH_DNS}
		EOF
        	cat ${ETH_NAME}
        	#service network restart
	fi
fi
